from aiogram import F
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.kbd import Button, Cancel, Start

from src.bot.dialogs.extras import copy_start_data_to_ctx
from src.bot.dialogs.getters.calendars import calendar_details_getter
from src.bot.dialogs.getters.users import user_info_getter
from src.bot.dialogs.widgets import LocaleText, StartWithData
from src.bot.states.user import CalendarDetailsSG, ChangeUserTimeSG

from .handlers import assign_calendar_to_user

calendar_details_window = Window(
    LocaleText("calendar-details"),
    LocaleText(
        "calendar-title", calendar_title="{calendar_title}", when=F["calendar_title"]
    ),
    LocaleText(
        "calendar-description",
        calendar_description="{calendar_description}",
        when=F["calendar_description"],
    ),
    LocaleText("calendar-status", status="{status}", when=F["status"]),
    LocaleText(
        "successful-tasks", tasks_count="{successful_tasks}", when=F["successful_tasks"]
    ),
    LocaleText("total-tasks", tasks_count="{total_tasks}", when=F["total_tasks"]),
    LocaleText("get-tasks-time", time="{get_tasks_time}", when=F["get_tasks_time"]),
    LocaleText(
        "check-tasks-time", time="{check_tasks_time}", when=F["check_tasks_time"]
    ),
    Button(
        LocaleText("select-calendar-btn"),
        id="select_calendar",
        on_click=assign_calendar_to_user,
        when=(~F["user_calendar_id"]) & (F["user_have_subscription"]),
    ),
    StartWithData(
        LocaleText("change-get-task-time-btn"),
        id="get_task",
        state=ChangeUserTimeSG.enter_new_time,
        when=(F["user_have_subscription"]) & (F["user_calendar_id"]),
        data={"time_to_change": "send_task_time"},
        data_keys=["user_calendar_id"],
    ),
    StartWithData(
        LocaleText("change-check-task-time-btn"),
        id="check_task",
        state=ChangeUserTimeSG.enter_new_time,
        when=(F["user_have_subscription"]) & (F["user_calendar_id"]),
        data={"time_to_change": "check_task_time"},
        data_keys=["user_calendar_id"],
    ),
    Cancel(LocaleText("back-btn")),
    state=CalendarDetailsSG.calendar_details,
    getter=(calendar_details_getter, user_info_getter),
)

calendar_details_dialog = Dialog(
    calendar_details_window,
    on_start=copy_start_data_to_ctx,
)
